<template>
  <el-card>
    <el-checkbox
      :indeterminate="isIndeterminate"
      v-model="checkAll"
      @change="handleCheckAllChange"
      >全选</el-checkbox
    >
    <div style="margin: 15px 0"></div>
    <el-checkbox-group
      v-model="checkedCities"
      @change="handleCheckedCitiesChange"
    >
      <el-checkbox
        v-for="item in cities"
        :label="item.userNo"
        :key="item.userNo"
        >{{ item.userName }}</el-checkbox
      >
    </el-checkbox-group>
  </el-card>
</template>

<script>
import Zujian from './zujian.vue'

const cityOptions = [
  {
    userNo: 'd50ea9110f0e48309e87a3b2753f7a11',
    userName: 'gaonana',
    userProjectRoleNo: null
  },
  {
    userNo: '4c0f61c1143d416d89ad4f3a67c77853',
    userName: 'admin',
    userProjectRoleNo: null
  },
  {
    userNo: '89b6cc7774ff4fe58e4f2bbe2b5d6ea8',
    userName: 'wanl',
    userProjectRoleNo: null
  },
  {
    userNo: '0879e06e533a449cb19eea63f024d193',
    userName: 'thinkgem',
    userProjectRoleNo: null
  }
]
export default {
  components: {
    Zujian
  },
  data () {
    return {
      checkAll: false,
      checkedCities: [],
      cities: cityOptions,
      isIndeterminate: true // 设置 indeterminate 状态，只负责样式控制
    }
  },
  watch: {
    checkAll (newVal) {
      console.log(newVal)
    }
  },
  methods: {
    handleCheckAllChange (val) {
      console.log(val)
      let arr = cityOptions.map((ite) => ite.userNo)
      this.checkedCities = val ? arr : []
      this.isIndeterminate = false
    },
    handleCheckedCitiesChange (value) {
      let checkedCount = value.length
      this.checkAll = checkedCount === this.cities.length
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length
    }
  }
}
</script>

<style>
</style>
